package com.softa.web.common.controller;

import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.security.token.SSOToken;
import com.softa.common.service.UserService;
import com.softa.common.sysconfig.shiro.authc.CaptchaAuthenticationToken;
import com.softa.common.sysconfig.support.LoginRequest;
import com.softa.common.sysconfig.support.Result;
import com.softa.common.utils.IpUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
* @ClassName: LoginController
* @Description: TODO
* @author yangl
* @date 2015-6-5 下午1:35:36
*
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @GetMapping()
    public String showLoginForm(HttpServletRequest req, Model model) {
        String exceptionClassName = (String)req.getAttribute("shiroLoginFailure");
        String error = null;
        if(UnknownAccountException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if(IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if(exceptionClassName != null) {
            error = "其他错误：" + exceptionClassName;
        }
        model.addAttribute("error", error);
        return "login";
    }

    @RequestMapping(value = "/checkAdminLogin", method = RequestMethod.POST)
    @ResponseBody
    public Result checkAdminLogin(HttpServletRequest req, Model model, LoginRequest loginReq) {
        //成功
        Integer status = 1;
        String message = "登录成功";
        //请输入用户名或密码
        if(StringUtils.isEmpty(loginReq.getUserName()) ||
                StringUtils.isEmpty(loginReq.getPassword())){
            status =  0;
            message = "请输入用户名或密码";
        }else{
            String ip = IpUtil.getIpAddr(req);
            CaptchaAuthenticationToken token = new CaptchaAuthenticationToken(loginReq.getUserName(), loginReq.getPassword(),
                    false, ip, null);
            token.setLoginType(loginReq.getLoginType());
            Subject subject = SecurityUtils.getSubject();
            try {
                //登录，即身份验证
                subject.login(token);
            } catch (AuthenticationException e) {
                //身份验证失败
//            logger.error("身份验证失败，异常：{}", e.getMessage());
                e.printStackTrace();
                status = 2;
                message = "身份验证失败";
            }
        }
        return Result.toSucess(status, message);
    }

    @RequestMapping(value = "/checkLogin", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("登录")
    public Result checkLogin(HttpServletResponse response, HttpServletRequest request, LoginRequest loginReq) {
        SSOHelper.clearLogin(request,response);
        Result result = userService.login(response, request, loginReq);
        return result;
    }



}
